﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.InterferenceVolume.CApIVSweptVolumeValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.InterferenceVolume.Res;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.InterferenceVolume;

internal class CApIVSweptVolumeValidator : ITxValidator
{
  private bool m_validateEmptyName = true;
  private string m_invalidSweptVolumeMessage;
  private string m_invalidTextMessage;
  private string m_invalidObjMessage;
  private string m_missingObjMessage;

  internal CApIVSweptVolumeValidator()
  {
    this.m_invalidSweptVolumeMessage = CUiIVInterferenceVolumeResourceTable.UI_INVALID_SWEPT_VOLUME;
    this.m_invalidTextMessage = CUiIVInterferenceVolumeResourceTable.UI_INVALID_OBJECT_NAME;
    this.m_invalidObjMessage = CUiIVInterferenceVolumeResourceTable.UI_INVALID_OBJECT;
    this.m_missingObjMessage = CUiIVInterferenceVolumeResourceTable.UI_EMPTY_SWEPT_VOLUME_NAME;
  }

  internal CApIVSweptVolumeValidator(bool validateEmptyName)
  {
    this.m_validateEmptyName = false;
    this.m_invalidSweptVolumeMessage = CUiIVInterferenceVolumeResourceTable.UI_INVALID_SWEPT_VOLUME;
    this.m_invalidTextMessage = CUiIVInterferenceVolumeResourceTable.UI_INVALID_OBJECT_NAME;
    this.m_invalidObjMessage = CUiIVInterferenceVolumeResourceTable.UI_INVALID_OBJECT;
    this.m_missingObjMessage = CUiIVInterferenceVolumeResourceTable.UI_EMPTY_SWEPT_VOLUME_NAME;
  }

  internal CApIVSweptVolumeValidator(
    string invalidSweptVolumeMessage,
    string invalidTextMessage,
    string invalidObjMessage,
    string missingObjMessage)
  {
    this.m_invalidSweptVolumeMessage = invalidSweptVolumeMessage;
    this.m_invalidTextMessage = invalidTextMessage;
    this.m_invalidObjMessage = invalidObjMessage;
    this.m_missingObjMessage = missingObjMessage;
  }

  internal CApIVSweptVolumeValidator(
    bool validateEmptyName,
    string invalidSweptVolumeMessage,
    string invalidTextMessage,
    string invalidObjMessage,
    string missingObjMessage)
  {
    this.m_validateEmptyName = validateEmptyName;
    this.m_invalidSweptVolumeMessage = invalidSweptVolumeMessage;
    this.m_invalidTextMessage = invalidTextMessage;
    this.m_invalidObjMessage = invalidObjMessage;
    this.m_missingObjMessage = missingObjMessage;
  }

  internal bool ValidateEmptyName
  {
    get => this.m_validateEmptyName;
    set => this.m_validateEmptyName = value;
  }

  public ITxObject GetObject(string text)
  {
    ITxObject itxObject1 = (ITxObject) null;
    if (text != null && text.Length > 0)
    {
      TxObjectList objectsByName = TxApplication.ActiveDocument.GetObjectsByName(text);
      if (objectsByName != null && ((Collection<ITxObject>) objectsByName).Count > 0)
      {
        string errorMessage = (string) null;
        foreach (ITxObject itxObject2 in (Collection<ITxObject>) objectsByName)
        {
          if (this.IsValidObject(itxObject2, out errorMessage))
          {
            itxObject1 = itxObject2;
            break;
          }
        }
      }
    }
    return itxObject1;
  }

  public string GetText(ITxObject obj)
  {
    string text = (string) null;
    try
    {
      text = obj.Name;
    }
    catch (Exception ex)
    {
    }
    return text;
  }

  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    using (new TxCallMessages.Collector((object) "CApIVSweptVolumeValidator.IsValidObject"))
    {
      bool flag = false;
      errorMessage = (string) null;
      if (obj != null)
      {
        if (this.IsSweptVolume(obj) && (obj as TxSweptVolume).HoldingObject.CanBeModified)
        {
          flag = true;
        }
        else
        {
          string text = this.GetText(obj) ?? "";
          errorMessage = !TxCallMessages.IsEmpty() ? string.Join("\n", TxCallMessages.GetMessages()) : string.Format(this.m_invalidSweptVolumeMessage, (object) this.InDoubleQuotes(text));
        }
      }
      else
        errorMessage = this.m_missingObjMessage;
      return flag;
    }
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    errorMessage = (string) null;
    bool flag = false;
    if (text == "")
    {
      if (!this.ValidateEmptyName)
        errorMessage = this.m_missingObjMessage;
      else
        flag = true;
    }
    else if (this.GetObject(text) != null)
      flag = true;
    else
      errorMessage = !this.DoesExistObjectByName(text) ? this.m_invalidTextMessage : string.Format(this.m_invalidObjMessage, (object) this.InDoubleQuotes(text));
    return flag;
  }

  private bool IsSweptVolume(ITxObject obj) => obj != null && obj is TxSweptVolume;

  private bool DoesExistObjectByName(string text)
  {
    bool flag = false;
    if (text != null && text.Length > 0)
    {
      TxObjectList objectsByName = TxApplication.ActiveDocument.GetObjectsByName(text);
      flag = objectsByName != null && ((Collection<ITxObject>) objectsByName).Count > 0;
    }
    return flag;
  }

  private string InDoubleQuotes(string text) => $"\"{text}\"";
}
